using Cysharp.Threading.Tasks;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using NetProtocol.Client;
using NetProtocol.POD;
using System.Collections.Generic;

namespace IQIGame.Onigao.GamePlay
{
    public partial class MainCityModule : CNetMainCity
    {
        public static CNetMainCity NetCore => Instance;

        public void InitModule(PlayerPOD playerPOD)
        {
            var data = MainCityData.Create(playerPOD.mainCityPOD);
            GameDataCenter.PlayerData.mainCityData = data;
        }

        public void ClearModule()
        {

        }

        ISession CNetMainCity.GetSession()
        {
            return GameServerModule.Instance.CurrSession;
        }

        #region private logic

        /// <summary>
        /// 更新客户端系统数据
        /// </summary>
        /// <param name="regionCid"></param>
        /// <param name="npcPOD"></param>
        private void UpdateNpcDataInDataCenter(int regionCid, MainCityNpcPOD npcPOD)
        {
            var maincityData = GameDataCenter.PlayerData.mainCityData;
            if (maincityData.regions.TryGetValue(regionCid, out var regionData))
            {
                if (regionData.npcs.TryGetValue(npcPOD.id, out var npcData))
                {
                    npcData.Init(npcPOD);
                }
                else
                {
                    npcData = new MainCityNpcData();
                    npcData.Init(npcPOD);
                    regionData.npcs.Add(npcData.id, npcData);
                }
            }
        }

        #endregion private logic

        void CNetMainCity.SC_notifyWeather(ISession session, int weatherCid)
        {
            var curArea = GameplayAreaManager.Instance.curSceneArea;
            var data = GameDataCenter.PlayerData.mainCityData;
            var region = data.regions[curArea.curRegionCid];
            region.weatherCid = weatherCid;
            //todo 主城天气表现还未实现
        }

        void CNetMainCity.SC_notifyNpcUpdate(ISession session, int regionCid, MainCityNpcPOD npcPOD)
        {
            UpdateNpcDataInDataCenter(regionCid, npcPOD);
            if (!isInMainCity)
            {
                return;
            }
            mainController.regionCtrl.entityCtrl.UpdateEntity(regionCid, npcPOD.id);
        }

        void CNetMainCity.SC_notifyNpcDrop(ISession session, int regionCid, int entityMcid, List<ItemShowPOD> items)
        {
            if (!isInMainCity)
            {
                return;
            }
            if (regionCid != mainController.regionCtrl.dynRegion.cfgRegion.Id)
            {
                return;
            }
            if (entityMcid != 0)
            {
                mainController.regionCtrl.entityCtrl.ShowEntityDrop(entityMcid, items).Forget();
            }
            if (items != null)
            {
                foreach (var item in items)
                {
                    PODPool<ItemShowPOD>.Put(item);
                }
                items.Clear();
            }
        }

        void CNetMainCity.SC_notifyNpcExecution(ISession session, int regionCid, int entityMcid, int executionId)
        {
            if (!isInMainCity)
            {
                return;
            }
            var cfgExecution = TableCenter.nonLevelExecution.Get(executionId);
            var exec = MainCityServiceFactory.CreateExecution(cfgExecution);
            if (exec != null)
            {
                exec.InitHost(Instance.GetEntityByMcid(entityMcid));
                exec.Execute();
                MainCityServiceFactory.ReleaseExecution(exec);
            }
        }
    }
}
